﻿
--lấy mã tự tang của cot  MaPhong trong bảng Phongchieu
CREATE FUNCTION func_SoTuTang()
RETURNS char(10)
AS
BEGIN
	declare @MaPhong char(10), @sotang int
	select @MaPhong = max(MaPhongChieu) from PhongChieu
	set @sotang =  cast( substring(@MaPhong,3, len(@MaPhong)) as int) +1 --Right(CAST(@MaPhong as char(5)),2)
	set @MaPhong = 'PC' + RIGHT('00' + CAST(@sotang  AS VARCHAR(5)), 5);
  RETURN @MaPhong
END
go
-- them phong chieu
create proc usp_ThemPhongChieu @SoHang int, @SoDay int, @MoTaPC nvarchar(Max), 
								@MaRap Char(10), @MaTT char(10), @MaDinhDangPhim char(10), @kq int out
As
begin	
		declare @MaPhong char(10)
		select @MaPhong = dbo.func_SoTuTang()
		INSERT into PhongChieu values(@MaPhong, @SoHang, @SoDay, @MoTaPc, @MaRap, @MaTT, @MaDinhDangPhim)
		if(EXISTS(select * from PhongChieu Where MaPhongChieu = @MaPhong))
			set @kq = 0
		else
			set @kq = 1
end
go
-- update phong